from fastmcp import FastMCP, Client
import segno
from dotenv import load_dotenv
import os
import asyncio
from config import (
    my_city,
    my_keywords,
    my_address,
    my_days,
    my_destination,
    my_origin,
    my_dlat,
    my_dlon,
    my_dname,
    my_slat,
    my_slon,
    my_sname,
)

# ref_mcp_server, where you can call amap MCP tools
load_dotenv(verbose=True)
ref_mcp_server_base_url = os.getenv('REF_MCP_SERVER_BASE_URL', default='https://mcp.amap.com/sse?key=')
amap_key = os.getenv('AMAP_KEY', default='')
ref_mcp_server_url = ref_mcp_server_base_url + amap_key
ref_client = Client(ref_mcp_server_url)


# async def ref_maps_weather(city:str=my_city):
#     async with ref_client:
#         print(f"Client {ref_client.transport} connected: {ref_client.is_connected()}")
#         # # Make MCP calls within the context
#         # tools = await ref_client.list_tools()
#         # print(f"Available tools: {tools}")

#         result = await ref_client.call_tool("maps_weather", {"city": city})
#         print(result)
#         # Connection is closed automatically here
#         print(f"Client {ref_client.transport} closed: {ref_client.is_connected()}")
#         return result
    
async def ref_maps_weather(city:str=my_city):
    async with ref_client:
        print(f"Client {ref_client.transport} connected: {ref_client.is_connected()}")
        # # Make MCP calls within the context
        # tools = await ref_client.list_tools()
        # print(f"Available tools: {tools}")

        result = await ref_client.call_tool("maps_weather", {"city": city})
        print(result)
        # Connection is closed automatically here
        print(f"Client {ref_client.transport} closed: {ref_client.is_connected()}")
        return result
    
async def ref_maps_text_search(city:str=my_city, keywords:str=my_keywords):
    async with ref_client:
        print(f"Client {ref_client.transport} connected: {ref_client.is_connected()}")
        result = await ref_client.call_tool("maps_text_search", {"city": city, "keywords": keywords})
        print(result)
        # Connection is closed automatically here
        print(f"Client {ref_client.transport} closed: {ref_client.is_connected()}")
        return result
    
async def ref_maps_geo(address:str=my_address):
    async with ref_client:
        print(f"Client {ref_client.transport} connected: {ref_client.is_connected()}")
        result = await ref_client.call_tool("maps_geo", {"address": address})
        print(result)
        # Connection is closed automatically here
        print(f"Client {ref_client.transport} closed: {ref_client.is_connected()}")
        return result
    
async def ref_maps_direction_bicycling(destination:str=my_destination, origin:str=my_origin):
    async with ref_client:
        print(f"Client {ref_client.transport} connected: {ref_client.is_connected()}")
        result = await ref_client.call_tool("maps_direction_bicycling", {"destination": destination, 'origin': origin})
        print(result)
        # Connection is closed automatically here
        print(f"Client {ref_client.transport} closed: {ref_client.is_connected()}")
        return result
    
async def ref_maps_direction_driving(destination:str=my_destination, origin:str=my_origin):
    async with ref_client:
        print(f"Client {ref_client.transport} connected: {ref_client.is_connected()}")
        result = await ref_client.call_tool("maps_direction_driving", {"destination": destination, 'origin': origin})
        print(result)
        # Connection is closed automatically here
        print(f"Client {ref_client.transport} closed: {ref_client.is_connected()}")
        return result
    
async def ref_maps_direction_transit_integrated(destination:str=my_destination, origin:str=my_origin, city:str=my_city, cityd:str=my_city):
    async with ref_client:
        print(f"Client {ref_client.transport} connected: {ref_client.is_connected()}")
        result = await ref_client.call_tool("maps_direction_transit_integrated", {"destination": destination, 'origin': origin, 'city': city, 'cityd': cityd})
        print(result)
        # Connection is closed automatically here
        print(f"Client {ref_client.transport} closed: {ref_client.is_connected()}")
        return result
    
async def ref_maps_direction_walking(destination:str=my_destination, origin:str=my_origin):
    async with ref_client:
        print(f"Client {ref_client.transport} connected: {ref_client.is_connected()}")
        result = await ref_client.call_tool("maps_direction_walking", {"destination": destination, 'origin': origin})
        print(result)
        # Connection is closed automatically here
        print(f"Client {ref_client.transport} closed: {ref_client.is_connected()}")
        return result
    
async def ref_maps_distance_driving(destination:str=my_destination, origin:str=my_origin):
    async with ref_client:
        print(f"Client {ref_client.transport} connected: {ref_client.is_connected()}")
        result = await ref_client.call_tool("maps_distance", {"destination": destination, 'origins': origin, 'type': 1})
        print(result)
        # Connection is closed automatically here
        print(f"Client {ref_client.transport} closed: {ref_client.is_connected()}")
        return result
    
async def ref_maps_distance_walking(destination:str=my_destination, origin:str=my_origin):
    async with ref_client:
        print(f"Client {ref_client.transport} connected: {ref_client.is_connected()}")
        result = await ref_client.call_tool("maps_distance", {"destination": destination, 'origins': origin, 'type': 3})
        print(result)
        # Connection is closed automatically here
        print(f"Client {ref_client.transport} closed: {ref_client.is_connected()}")
        return result
    
async def ref_maps_distance_direct(destination:str=my_destination, origin:str=my_origin):
    async with ref_client:
        print(f"Client {ref_client.transport} connected: {ref_client.is_connected()}")
        result = await ref_client.call_tool("maps_distance", {"destination": destination, 'origins': origin, 'type': 0})
        print(result)
        # Connection is closed automatically here
        print(f"Client {ref_client.transport} closed: {ref_client.is_connected()}")
        return result

async def ref_maps_schema_take_taxi(dlat:str=my_dlat, dlon:str=my_dlon, dname:str=my_dname, slat:str=my_slat, slon:str=my_slon, sname:str=my_sname)->str:
    async with ref_client:
        print(f"Client {ref_client.transport} connected: {ref_client.is_connected()}")
        result = await ref_client.call_tool("maps_schema_take_taxi", {'dlat':dlat, 'dlon':dlon, 'dname':dname, 'slat':slat, 'slon':slon, 'sname':sname})
        # print(result)
        output = result[0].text.split('唤端URI，直接完整展示，禁止任何加工')[1]
        print(output)
        # Connection is closed automatically here
        print(f"Client {ref_client.transport} closed: {ref_client.is_connected()}")
        return output

if __name__ == "__main__":
    # asyncio.run(ref_maps_weather(city='海南'))
    # asyncio.run(ref_maps_text_search(city='海南', keywords='景点'))
    # asyncio.run(ref_maps_geo(address='海口骑楼老街', city='海南'))
    # asyncio.run(ref_maps_direction_bicycling())
    # asyncio.run(ref_maps_direction_driving())
    # asyncio.run(ref_maps_direction_transit_integrated())
    # asyncio.run(ref_maps_direction_walking())
    # asyncio.run(ref_maps_distance_driving())
    # asyncio.run(ref_maps_distance_walking())
    # asyncio.run(ref_maps_distance_direct())
    asyncio.run(ref_maps_schema_take_taxi())
    pass
